package com.cskaoyan.service.system;

import com.cskaoyan.bean.BasePageInfo;
import com.cskaoyan.bean.po.adminpo.MarketAdmin;
import com.cskaoyan.bean.po.adminpo.MarketAdminExample;
import com.cskaoyan.bean.vo.system.*;
import com.cskaoyan.mapper.MarketAdminMapper;
import com.cskaoyan.util.beanutils.BeanUtil;
import com.cskaoyan.util.crypt.CryptUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.*;

/**
 * @auther zk
 * @date 2022/7/19 11:09
 */
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    MarketAdminMapper marketAdminMapper;

    @Override
    public AdminListVo adminList(BasePageInfo info, String username) {

        PageHelper.startPage(info.getPage(), info.getLimit());

        MarketAdminExample example = new MarketAdminExample();
        example.setOrderByClause(info.getSort() + " " + info.getOrder());
        MarketAdminExample.Criteria criteria = example.createCriteria();
        username = (username == null) ? "%" : ("%" + username + "%");
        criteria.andUsernameLike(username);
        criteria.andDeletedEqualTo(false);
        List<MarketAdmin> marketAdmins = marketAdminMapper.selectByExample(example);

        List<AdminInfo> adminInfos = new ArrayList<>();
        for (MarketAdmin marketAdmin : marketAdmins) {
            AdminInfo adminInfo = new AdminInfo();
            BeanUtil.copyBean(marketAdmin, adminInfo);
            adminInfos.add(adminInfo);
        }
        PageInfo<MarketAdmin> pi = new PageInfo<>(marketAdmins);

        return new AdminListVo((int) pi.getTotal(), pi.getPages(), pi.getPageSize(), pi.getPageNum(), adminInfos);
    }

    @Override
    public AdminInfo adminCreate(Map map) {

        // 查询该管理员是否存在
        MarketAdminExample example = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(map.get("username").toString());
        criteria.andDeletedEqualTo(false);
        List<MarketAdmin> marketAdmins = marketAdminMapper.selectByExample(example);
        if (marketAdmins.size() != 0) {
            return null;
        }

        // 不存在重复的情况下，插入管理员
        MarketAdmin marketAdmin = new MarketAdmin();
        marketAdmin.setUsername(map.get("username").toString());
        marketAdmin.setPassword(CryptUtils.encryptStr(map.get("password").toString()));
        if (map.get("avatar") != null) {
            marketAdmin.setAvatar(map.get("avatar").toString());
        }
        marketAdmin.setAddTime(new Date(System.currentTimeMillis()));
        marketAdmin.setUpdateTime(new Date(System.currentTimeMillis()));
        List<Integer> a = (ArrayList) map.get("roleIds");
        Integer[] roleIds = new Integer[a.size()];
        for (int i = 0; i < a.size(); i++) {
            roleIds[i] = a.get(i);
        }
        marketAdmin.setRoleIds(roleIds);
        marketAdmin.setDeleted(false);
        marketAdminMapper.insertSelective(marketAdmin);

        // 查出自增主键ID
        List<MarketAdmin> admins = marketAdminMapper.selectByExample(example);
        marketAdmin.setId(admins.get(0).getId());
        AdminInfo adminInfo = new AdminInfo();
        BeanUtil.copyBean(marketAdmin, adminInfo);

        return adminInfo;
    }

    @Override
    public AdminInfo adminUpdate(Map map) throws ParseException {

        // 查询该用户名是否是其它管理员的用户名
        MarketAdminExample example1 = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = example1.createCriteria();
        criteria.andUsernameEqualTo((String) map.get("username"));
        criteria.andIdNotEqualTo((Integer) map.get("id"));
        criteria.andDeletedEqualTo(false);
        List<MarketAdmin> marketAdmins = marketAdminMapper.selectByExample(example1);
        if (marketAdmins.size() >= 1) {
            return null;
        }

        // 不存在重复的情况下，更新管理员
        MarketAdmin marketAdmin = new MarketAdmin();
        marketAdmin.setUsername((String) map.get("username"));
        marketAdmin.setPassword(CryptUtils.encryptStr((String) map.get("password")));
        if (map.get("avatar") != null) {
            marketAdmin.setAvatar((String) map.get("avatar"));
        }
        marketAdmin.setUpdateTime(new Date(System.currentTimeMillis()));

        MarketAdminExample example2 = new MarketAdminExample();
        MarketAdminExample.Criteria criteria1 = example2.createCriteria();
        criteria1.andIdEqualTo((Integer) map.get("id"));
        marketAdminMapper.updateByExampleSelective(marketAdmin, example2);

        MarketAdmin admin = marketAdminMapper.selectByPrimaryKey((Integer) map.get("id"));
        AdminInfo adminInfo = new AdminInfo();
        BeanUtil.copyBean(admin, adminInfo);

        return adminInfo;
    }

    @Override
    public Integer adminDelete(Map map) {
        Integer id = (Integer) map.get("id");
        if (id == 1 || id == 2 || id == 3) {
            return 1;
        }
        MarketAdminExample example = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);
        MarketAdmin admin = new MarketAdmin();
        admin.setDeleted(true);
        marketAdminMapper.updateByExampleSelective(admin, example);
        return 0;
    }


}
